package com.ruoyi.whole.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.AutofillUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SerialNumberGenerator;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.whole.domain.DmsDealerVehicleInfo;
import com.ruoyi.whole.domain.DmsSaleReport;
import com.ruoyi.whole.domain.DmsSaleReportCustomerInfo;
import com.ruoyi.whole.mapper.DmsSaleReportMapper;
import com.ruoyi.whole.service.IDmsDealerVehicleInfoService;
import com.ruoyi.whole.service.IDmsSaleReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 实销上报Service业务层处理
 * 
 * @author liuchuanfa
 * @date 2024-03-10
 */
@Service
public class DmsSaleReportServiceImpl extends ServiceImpl<DmsSaleReportMapper,DmsSaleReport> implements IDmsSaleReportService
{
    @Autowired
    private DmsSaleReportMapper dmsSaleReportMapper;

    @Autowired
    private AutofillUtil autofillUtil;

    @Autowired
    private IDmsDealerVehicleInfoService iDmsDealerVehicleInfoService;

    @Autowired
    private ISysDeptService iSysDeptService;

    @Autowired
    SerialNumberGenerator serialNumberGenerator;

    /**
     * 查询实销上报
     * 
     * @param id 实销上报主键
     * @return 实销上报
     */
    @Override
    public DmsSaleReport selectDmsSaleReportById(Long id)
    {
        return dmsSaleReportMapper.selectDmsSaleReportById(id);
    }

    /**
     * 查询实销上报列表
     * 
     * @param dmsSaleReport 实销上报
     * @return 实销上报
     */
    @Override
    public List<DmsSaleReport> selectDmsSaleReportList(DmsSaleReport dmsSaleReport)
    {
        return dmsSaleReportMapper.selectDmsSaleReportList(dmsSaleReport);
    }

    /**
     * 新增实销上报
     * 
     * @param dmsSaleReport 实销上报
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertDmsSaleReport(DmsSaleReport dmsSaleReport)
    {
        //已实销上报的vin，需要更改状态为已实销上报
        String vin = dmsSaleReport.getVehicleVin();
        iDmsDealerVehicleInfoService.lambdaUpdate().set(DmsDealerVehicleInfo::getVehicleStatus,3).eq(DmsDealerVehicleInfo::getVin,vin).update();

        //若没有整车零售单号就自动生成
        if (StrUtil.isBlank(dmsSaleReport.getRetailOrderNo())){
            dmsSaleReport.setRetailOrderNo(serialNumberGenerator.generateSerialNumber("ZCLSDH"));
        }
        //添加当前经销商id name
        dmsSaleReport.setDealerId(SecurityUtils.getLoginUser().getUser().getDept().getParentId());
        dmsSaleReport.setDealerName(iSysDeptService.selectDeptById(SecurityUtils.getLoginUser().getUser().getDept().getParentId()).getDeptName());
        //添加实销上人id name time
        dmsSaleReport.setReportPersonId(SecurityUtils.getLoginUser().getUser().getUserId());
        dmsSaleReport.setReportPersonName(SecurityUtils.getLoginUser().getUser().getUserName());
        dmsSaleReport.setReportDate(new Date());
        //新增自动填充
        autofillUtil.autoFillCreate(dmsSaleReport);
        int rows = dmsSaleReportMapper.insertDmsSaleReport(dmsSaleReport);
        insertDmsSaleReportCustomerInfo(dmsSaleReport);
        return rows;
    }

    /**
     * 修改实销上报
     * 
     * @param dmsSaleReport 实销上报
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDmsSaleReport(DmsSaleReport dmsSaleReport)
    {
        autofillUtil.autoFillUpdate(dmsSaleReport);
        dmsSaleReportMapper.deleteDmsSaleReportCustomerInfoBySalesReportId(dmsSaleReport.getId());
        //修改时不允许修改填过的用户信息，所以注释插入代码
//        insertDmsSaleReportCustomerInfo(dmsSaleReport);
        return dmsSaleReportMapper.updateDmsSaleReport(dmsSaleReport);
    }

    /**
     * 批量删除实销上报
     * 
     * @param ids 需要删除的实销上报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsSaleReportByIds(Long[] ids)
    {
        dmsSaleReportMapper.deleteDmsSaleReportCustomerInfoBySalesReportIds(ids);
        return dmsSaleReportMapper.deleteDmsSaleReportByIds(ids);
    }

    /**
     * 删除实销上报信息
     * 
     * @param id 实销上报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsSaleReportById(Long id)
    {
        dmsSaleReportMapper.deleteDmsSaleReportCustomerInfoBySalesReportId(id);
        return dmsSaleReportMapper.deleteDmsSaleReportById(id);
    }

    /**
     * 新增实销上报客户信息信息
     * 
     * @param dmsSaleReport 实销上报对象
     */
    public void insertDmsSaleReportCustomerInfo(DmsSaleReport dmsSaleReport)
    {
        List<DmsSaleReportCustomerInfo> dmsSaleReportCustomerInfoList = dmsSaleReport.getDmsSaleReportCustomerInfoList();
        Long id = dmsSaleReport.getId();
        if (StringUtils.isNotNull(dmsSaleReportCustomerInfoList))
        {
            List<DmsSaleReportCustomerInfo> list = new ArrayList<DmsSaleReportCustomerInfo>();
            for (DmsSaleReportCustomerInfo dmsSaleReportCustomerInfo : dmsSaleReportCustomerInfoList)
            {
                //设置实销上报单的id
                dmsSaleReportCustomerInfo.setSalesReportId(id);
                //新增自动填充
                autofillUtil.autoFillCreate(dmsSaleReportCustomerInfo);
                list.add(dmsSaleReportCustomerInfo);
            }
            if (list.size() > 0)
            {
                dmsSaleReportMapper.batchDmsSaleReportCustomerInfo(list);
            }
        }
    }
}
